package cn.hdu.liang.socket;

import java.io.*;
import java.net.Socket;

public class SocketTest1 {
    public static void main(String[] args) {
        String host = "www.baidu.com";
        String protocol = "http";
        try {
            Socket socket = new Socket(host, 80);
            OutputStream os = socket.getOutputStream();
            boolean autoflush = true;
            PrintWriter out = new PrintWriter(socket.getOutputStream(), autoflush);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            out.println("GET / HTTP/1.1");
            out.println("Host: " + host + ":80");
            out.println("Connection: Close");
            out.println();

            boolean loop = true;
            StringBuilder sb = new StringBuilder(8096);
            while(loop) {
                if(in.ready()) {
                    int i = 0;
                    while(i != -1) {
                        i = in.read();
                        sb.append((char) i);
                    }
                    loop = false;
                }
            }

            System.out.println(sb.toString());
            socket.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}